﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.effects {
    import Engine.Events.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Classes.*;
    import com.greensock.*;
    import Engine.*;

    public class CelebrationBalloonsEffect extends AnimationEffect {

        private const ANIM_DURATION:Number = 1;
        private const TARGET_PADDING_Y:Number = -20;
        private const BALLOON_OFFSET_Y:Number = -8;

        private var m_balloons:Array;
        private var m_isAnimating:Boolean;
        private var m_itemImageLoadedHash:Object;
        private var m_itemImageInstances:Array;
        private var m_isItemImagesLoaded:Boolean = false;

        public function CelebrationBalloonsEffect(_arg1:MapResource, _arg2:String, _arg3:Boolean=true){
            this.m_isAnimating = _arg3;
            super(_arg1, _arg2);
        }
        override public function cleanUp():void{
            var _local1:DisplayObject;
            if (this.m_balloons){
                for each (_local1 in this.m_balloons) {
                    this.cleanUpBalloon(_local1);
                };
            };
            this.m_balloons = null;
            super.cleanUp();
        }
        override public function reattach():void{
            var _local10:Number;
            var _local11:Number;
            var _local12:Number;
            var _local1:DisplayObject = m_mapResource.content;
            if (((!(this.m_isItemImagesLoaded)) || (!(_local1)))){
                return;
            };
            var _local2:Vector3 = m_mapResource.getReference().getSize();
            var _local3:IsoRect = IsoRect.getIsoRectFromSize(_local2);
            var _local4:Number = 0;
            var _local5:Number = (-(m_mapResource.displayObjectOffsetY) / m_mapResource.displayObject.scaleY);
            var _local6:Sprite = getAttachParent();
            var _local7:Number = ((_local5 - _local3.height) + (_local3.height - _local3.bottom.y));
            var _local8:Point = new Point();
            _local8.x = _local4;
            _local8.y = (_local7 + this.BALLOON_OFFSET_Y);
            this.m_balloons = [];
            this.makeBalloon(_local3.left, _local8, _local6);
            this.makeBalloon(_local3.right, _local8, _local6);
            this.makeBalloon(_local3.bottom, _local8, _local6);
            var _local9:DisplayObject = this.makeBalloon(_local3.top, _local8);
            _local6.addChildAt(_local9, _local1.parent.getChildIndex(_local1));
            if (this.m_isAnimating){
                _local10 = ((-(_local5) + this.TARGET_PADDING_Y) + _local7);
                for each (_local9 in this.m_balloons) {
                    _local11 = (MathUtil.random(3) * 0.1);
                    _local12 = ((_local9.y + _local10) + MathUtil.random(4));
                    TweenLite.to(_local9, this.ANIM_DURATION, {
                        y:_local12,
                        delay:_local11
                    });
                    TweenLite.to(_local9, 0.5, {
                        alpha:0,
                        delay:(_local11 + this.ANIM_DURATION),
                        overwrite:0,
                        onComplete:this.tweenCompleteHandler,
                        onCompleteParams:[_local9]
                    });
                };
            };
            m_isActive = false;
        }
        private function makeBalloon(_arg1:Point, _arg2:Point=null, _arg3:DisplayObjectContainer=null):DisplayObject{
            _arg2 = ((_arg2) || (new Point()));
            var _local4:DisplayObject = this.getBalloonInstance();
            this.m_balloons[this.m_balloons.length] = _local4;
            _local4.x = ((_arg1.x + _arg2.x) - (_local4.width >> 1));
            _local4.y = ((_arg1.y + _arg2.y) - _local4.height);
            if (_arg3){
                _arg3.addChild(_local4);
            };
            return (_local4);
        }
        private function getBalloonInstance():DisplayObject{
            var _local1:int;
            var _local2:ItemImageInstance = this.m_itemImageInstances[_local1];
            if (this.m_itemImageInstances.length > 1){
                this.m_itemImageInstances.splice(_local1, 1);
            };
            var _local3:DisplayObject = (_local2.image as DisplayObject);
            return (_local3);
        }
        override protected function loadAnimationEffect():void{
            var _local2:XML;
            var _local3:XMLList;
            var _local4:XML;
            var _local5:ItemImage;
            var _local6:String;
            var _local1:Boolean = ((((m_mapResource.content) && (m_mapResource.getItemImage()))) && (!(m_mapResource.isCurrentImageLoading())));
            if (((_local1) && (!(m_itemImage)))){
                _local2 = Global.gameSettings.getEffectByName(m_effectName);
                _local3 = _local2.image;
                this.m_itemImageLoadedHash = {};
                for each (_local4 in _local3) {
                    _local6 = _local4.attribute("name");
                    this.m_itemImageLoadedHash[_local6] = false;
                };
                this.m_itemImageInstances = [];
                for each (_local4 in _local3) {
                    _local5 = new ItemImage(_local4);
                    _local5.addEventListener(LoaderEvent.LOADED, this.onItemImageLoaded, false, 0, true);
                    _local5.load();
                };
            };
        }
        override protected function onItemImageLoaded(_arg1:LoaderEvent):void{
            var _local2:ItemImage;
            var _local3:ItemImageInstance;
            var _local4:String;
            var _local5:Boolean;
            var _local6:Boolean;
            if (!this.m_isItemImagesLoaded){
                _local2 = (_arg1.target as ItemImage);
                _local2.removeEventListener(LoaderEvent.LOADED, this.onItemImageLoaded);
                _local3 = _local2.getInstance();
                if (!_local3){
                    return;
                };
                this.m_itemImageInstances[this.m_itemImageInstances.length] = _local3;
                _local4 = _local2.name;
                this.m_itemImageLoadedHash[_local4] = true;
                _local5 = true;
                for each (_local6 in this.m_itemImageLoadedHash) {
                    if (!_local6){
                        _local5 = false;
                        break;
                    };
                };
                if (_local5){
                    this.m_isItemImagesLoaded = true;
                    this.reattach();
                };
            };
        }
        private function cleanUpBalloon(_arg1:DisplayObject):void{
            if (_arg1){
                TweenLite.killTweensOf(_arg1);
                if (_arg1.parent){
                    _arg1.parent.removeChild(_arg1);
                };
            };
        }
        private function tweenCompleteHandler(_arg1:DisplayObject):void{
            var _local2:int;
            this.cleanUpBalloon(_arg1);
            if (this.m_balloons){
                _local2 = this.m_balloons.indexOf(_arg1);
                if (_local2 >= 0){
                    this.m_balloons.splice(_local2, 1);
                };
            };
            if (((!(this.m_balloons)) || ((this.m_balloons.length <= 0)))){
                this.cleanUp();
            };
        }

    }
}//package Classes.effects 
